import { banner, recommend_list} from "../../network/api";
import http from "../../network/http";

export default {
  namespaced: true,
  state: {
    banner: [],
    recommend: [],
    bannerLoading: false,
    recommendLoading: false,
  },
  getters: {
    loading(state) {
      return state.bannerLoading || state.recommendLoading;
    },
  },
  mutations: {
    //轮播图loading
    setBannerLoading(state, payload) {
      state.bannerLoading = payload;
    },
    setBanner(state, payload) {
      state.banner = payload;
    },
    //推荐歌单loading
    setRecommendLoading(state, payload) {
      state.recommendLoading = payload;
    },
    //推荐歌单列表
    setRecommendList(state, payload) {
      state.recommend = payload;
    },
   
  },
  actions: {
    //获取轮播图数据
    async getBanner(context) {
      context.commit("setBannerLoading", true);
      http.get(banner).then((res) => {
        let { banners } = res;
        let newData = banners.map((item) => item.imageUrl);
        context.commit("setBanner", newData);
        context.commit("setBannerLoading", false);
      });
    },
    //获取推荐列表
    async getRecommendList(context) {
      context.commit("setRecommendLoading", true);
      let { result } = await http.get(recommend_list);
      let newData = result.map(({ id, name, picUrl, playCount }) => ({
        id,
        name,
        picUrl,
        playCount,
      }));
      context.commit("setRecommendLoading", false);
      context.commit("setRecommendList", newData);
    },
   
  },
};
